﻿using GreenXi.Help;
using GreenXi.Help.Dam;
using GreenXi.Help.Plc;
using GreenXi.Help.PLC;
using GreenXi.Pages.Navig;
using GreenXi.ViewModel.Utils;
using PressSystem_Can.Help.Can;
using Quartz;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace GreenXi.Job
{
    /// <summary>
    /// 心跳
    /// </summary>
    [DisallowConcurrentExecution]
    public class HeartBeatJob : IJob
    {
        public static ITrigger GetTrigger()
        {
            return TriggerBuilder.Create().WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromMilliseconds(1000)).RepeatForever())
            .Build();
        }

        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                ///获取实时数据
                foreach (var item in GlobalVariables.CraftViewList)
                {
                    if ((string.IsNullOrEmpty(item.NodeId) || string.IsNullOrEmpty(item.StationNode) || item.GateWayId == null)&& item.Type != 3)
                    {
                        continue;
                    }
                    if (item.Gather > item.GatherCount2 || item.Gather == null)
                    {
                        item.GatherCount2++;
                        continue;
                    }
                    item.GatherCount2 = 0;
                    if (item.Type != 3)
                    {
                        var Gateway = GlobalVariables.GatewayList.Where(x => x.Id == item.GateWayId).FirstOrDefault();
                        if (Gateway != null)
                        {
                           ModBus.GetElectricCurrent(item.NodeId, Gateway.IPId, item.StationNode);
                            ModBus.GetElectricEnergy(item.NodeId, Gateway.IPId, item.StationNode);
                        }

                    }
                    else//虚拟表实时数据从选中电表汇总
                    {
                        string[] substrings = item.NodeId.Split(',');
                        var listt = GlobalVariables.CraftViewList.Where(x => substrings.Contains(x.Id.ToString()));
                        item.Electric_A = listt.Sum(x => x.Electric_A);
                        item.Electric_B = listt.Sum(x => x.Electric_B);
                        item.Electric_C = listt.Sum(x => x.Electric_C);

                        item.PowerFactor_A = listt.Sum(x => x.PowerFactor_A);
                        item.PowerFactor_B = listt.Sum(x => x.PowerFactor_B);
                        item.PowerFactor_C = listt.Sum(x => x.PowerFactor_C);

                        item.Power_A = listt.Sum(x => x.Power_A);
                        item.Power_B = listt.Sum(x => x.Power_B);
                        item.Power_C = listt.Sum(x => x.Power_C);

                        item.Voltage_A = listt.Sum(x => x.Voltage_A);
                        item.Voltage_B = listt.Sum(x => x.Voltage_B);
                        item.Voltage_C = listt.Sum(x => x.Voltage_C);

                        item.Energy = Math.Round(listt.Sum(x => x.Energy),2);
                        item.Status = listt.FirstOrDefault()!=null? listt.FirstOrDefault().Status:0;

                    }
                }

               
                var lists = GlobalVariables.CraftViewList2; ;
                foreach (var item in lists)
                {
                    if ((string.IsNullOrEmpty(item.NodeId) || string.IsNullOrEmpty(item.StationNode) || item.GateWayId == null))
                    {
                        continue;
                    }
                    if (item.Gather > item.GatherCount2 || item.Gather == null)
                    {
                        item.GatherCount2++;
                        continue;
                    }
                    item.GatherCount2 = 0;
                    var Gateway = GlobalVariables.GatewayList.Where(x => x.Id == item.GateWayId).FirstOrDefault();
                    if (Gateway != null)
                    {
                        ModBus.GetTemperature(item.NodeId, Gateway.IPId, item.StationNode);
                    }
                }

                 lists = GlobalVariables.CraftViewList3; ;
                foreach (var item in lists)
                {
                    if ((string.IsNullOrEmpty(item.NodeId) || string.IsNullOrEmpty(item.StationNode) || item.GateWayId == null))
                    {
                        continue;
                    }
                    if (item.Gather > item.GatherCount2 || item.Gather == null)
                    {
                        item.GatherCount2++;
                        continue;
                    }
                    item.GatherCount2 = 0;
                    var Gateway = GlobalVariables.GatewayList.Where(x => x.Id == item.GateWayId).FirstOrDefault();
                    if (Gateway != null)
                    {
                        ModBus.GetTransformerTemperature(item.NodeId, Gateway.IPId, item.StationNode);
                    }
                }


                PressUtil pressUtil = new PressUtil();
                //去画线
                pressUtil.DrawlineAsync();
                //报警判定
                pressUtil.IsCallPoliceAsync();
            }
            catch(Exception ex)
            {
                Helpers.Logger.Instance.Error(ex,"读取数据错误");
            }
          
        }
       
    }
}
